﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.System.Dtos.ModuleColumn;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 功能列表
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.20
    /// </summary>
    [Route("api/System/[controller]")]
    public class ModuleColumnController : BaseApiController
    {
        private ModuleColumnBll moduleColumnBll = new ModuleColumnBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="moduleId">功能主键</param>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpGet("{moduleId}/Fields")]
        public async Task<ResponseResult> GetList(string moduleId, KeywordInput input)
        {
            var data = await moduleColumnBll.GetList(moduleId);
            if (!string.IsNullOrEmpty(input.keyword))
            {
                data = data.FindAll(t => t.EnCode.Contains(input.keyword) || t.FullName.Contains(input.keyword));
            }
            var listModuleColumn = data.MapToList<ModuleColumnListOutput>();
            return Success(new { list = listModuleColumn });
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = (await moduleColumnBll.GetInfo(id)).MapTo<ModuleColumnInfoOutput>();
            return Success(data);
        }
        #endregion

        #region POST
        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] ModuleColumnCrInput input)
        {
            var entitys = input.MapTo<ModuleColumnEntity>();
            var isOk = await moduleColumnBll.Create(entitys);
            return isOk > 0 ? Success("新建成功") : Error("新建失败");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] ModuleColumnUpInput input)
        {
            var entitys = input.MapTo<ModuleColumnEntity>();
            var isOk = await moduleColumnBll.Update(id, entitys);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await moduleColumnBll.GetInfo(id);
            if (entity != null)
            {
                var isOk = await moduleColumnBll.Delete(entity);
                return isOk > 0 ? Success("删除成功") : Error("删除失败");
            }
            else
            {
                return Success("删除失败");
            }
        }

        /// <summary>
        /// 批量新建
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpPost("Actions/Batch")]
        public async Task<ResponseResult> BatchCreate([FromBody] ModuleColumnActionsBatchInput input)
        {
            var entitys = JsonHelper.ToList<ModuleColumnEntity>(input.columnJson.ToString());
            var isOk = await moduleColumnBll.Create(entitys);
            return isOk > 0 ? Success("新建成功") : Error("新建失败");
        }

        /// <summary>
        /// 更新字段状态
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/State")]
        public async Task<ResponseResult> ActionsState(string id)
        {
            var entity = await moduleColumnBll.GetInfo(id);
            entity.EnabledMark = entity.EnabledMark == 0 ? 1 : 0;
            var isOk = await moduleColumnBll.Update(id, entity);
            return isOk > 0 ? Success("更新状态成功") : Error("更新状态失败");
        }
        #endregion
    }
}
